<?php

declare(strict_types=1);

namespace App\Model\Setting;

use App\Model\Service\CacheService;
use App\Model\UserProfile;
use Hyperf\DbConnection\Model\Model;

/**
 *  配置 - 用户等级（魅力豪气）
 */
class SettingUserLevel extends Model
{
    protected $table = 'setting_user_level';
    protected $fillable = ['level', 'title', 'number', 'level_type', 'icon', 'flag'];

    const TYPE_RICH = 1;
    const TYPE_CHARM = 2;

    static function getDistance($levelType = 1, $levelValue = 0)
    {
        $where = [
            ['level_type', $levelType],
            ['number', '>=', $levelValue],
            ['flag', 1]
        ];
        $data = self::where($where)->orderBy('number', 'asc')->first();
        return $data ? $data->number - $levelValue : 0;
    }

    /**
     * 筛选等级
     */
    static function pickLevel($value = 0, $setting = [])
    {
        $count = count($setting);
        foreach ($setting as $k => $item) {
            if ($value < $item['level_value']) return $item['level'] - 1;
            if ($k == $count - 1) return $item['level'];
        }
        return 0;
    }

    /**
     * 筛选魅力值
     */
    static function pickCharmLimitByCoin($coin = 0, $coinArr = [])
    {
        $count = count($coinArr);
        foreach ($coinArr as $k => $item) {
            if ($k == $count - 1 && $coin >= $item['coin']) return [
                'charm' => $item['charm_limit'],
                'level' => $item['level_limit'],
            ];
            if ($coin >= $item['coin'] && $coin < $coinArr[$k + 1]['coin']) {
                return [
                    'charm' => $item['charm_limit'],
                    'level' => $item['level_limit'],
                ];
            }
        }
        return ['charm' => 0, 'level' => 0];
    }

    /**
     * 获取用户等级
     */
    static function getUserLevel($profile = null)
    {
        if (is_array($profile)) {
            $role = $profile['role'];
            $charm = $profile['charm_value'];
            $rich = $profile['rich_value'];
        } else {
            $role = $profile->role;
            $charm = $profile->charm_value;
            $rich = $profile->rich_value;
        }
        //主播-魅力值
        if (UserProfile::checkRoleHost($role)) {
            $setting = CacheService::getUserLevelSetting(self::TYPE_CHARM);
            return self::pickLevel($charm, $setting);
        } else {
            $setting = CacheService::getUserLevelSetting(self::TYPE_RICH);
            return self::pickLevel($rich, $setting);
        }
        return 0;
    }

    /**
     * 查询收费设置
     */
    static function checkCoinSet($myCoinSet = 0, $myCharm = 0, $coinField = 'video_coin_setting')
    {
        $okRet = ['errcode' => 0];
        if ($myCoinSet == 0) return $okRet;
        $coinField == 'coin_setting' && $coinField = 'voice_coin_setting';
        $settings = CacheService::getUserLevelSetting(2);
        // $count = count($settings);
        $errRet = ['errcode' => 1, 'errmsg' => '魅力值不满足条件！'];
        foreach ($settings as $k => $setting) {
            if ($myCharm < $settings[$k + 1]['level_value']) {
                return $myCoinSet <= $setting[$coinField] ? $okRet : $errRet;
            }
        }
        return $okRet;
    }
}
